package com.weiaibenpao.demo.chislim.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.entity.LocalMedia;
import com.weiaibenpao.demo.chislim.R;

import java.util.List;

/**
 * Created by wlx on 2018/1/3.
 */

public class ReleaseAdapter extends RecyclerView.Adapter<ReleaseAdapter.ViewHolder> {

    private List<LocalMedia> mListUrl;

    private Context mContext;
    private OnItemClickListener onItemClickListener;

    public ReleaseAdapter(Context context) {
        this.mContext = context;
    }

    public void addDate(List<LocalMedia> mList) {
        this.mListUrl = mList;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.release_item_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
//                .placeholder(Color.parseColor("#f6f6f6"))
                .diskCacheStrategy(DiskCacheStrategy.ALL);
//        RequestOptions options = new RequestOptions();
//        options.placeholder(R.drawable.icon_add);
        if (mListUrl == null || mListUrl.size() == 0) {
            Glide.with(mContext).load(R.drawable.icon_add)
                    .into(holder.img);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onAddClick();
                }
            });
        } else if (mListUrl.size() >= 9) {
            Glide.with(mContext).load(mListUrl.get(position).getPath()).apply(options).into(holder.img);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(position);
                }
            });
        } else {
            if (position == mListUrl.size()) {
                Glide.with(mContext).load(R.drawable.icon_add).apply(options).into(holder.img);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onAddClick();
                    }
                });
            } else {

                Glide.with(mContext).load(mListUrl.get(position).getPath()).apply(options)
                        .into(holder.img);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClick(position);
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        if (mListUrl == null || mListUrl.size() == 0) {
            return 1;
        } else if (mListUrl.size() >= 9) {
            return mListUrl.size();
        }
        return mListUrl.size() + 1;
    }

    class ViewHolder extends RecyclerView.ViewHolder {


        private ImageView img;

        public ViewHolder(View itemView) {
            super(itemView);
            img = ((ImageView) itemView.findViewById(R.id.img_icon));

        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

        void onAddClick();
    }
}
